package com.zxc.service.impl;

import com.zxc.dao.*;
import com.zxc.model.Apply.ApplyWarehouse;
import com.zxc.model.Equipment.Equipment;
import com.zxc.model.Equipment.EquipmentApply;
import com.zxc.model.Fertilizer;
import com.zxc.model.Pesticide;
import com.zxc.model.Tools;
import com.zxc.model.seeds;
import com.zxc.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyDao applyDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private EquipmentDao equipmentDao;

    public int setApplyDate(ApplyWarehouse applyWarehouse){
//        申请订单的状态。1表示在审批。
        applyWarehouse.setState(1);
//        生成主键
        applyWarehouse.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//        申请时间
        Date d = new Date();
        SimpleDateFormat dqsj = new SimpleDateFormat("yyyy-MM-dd");
        applyWarehouse.setApplyTime(dqsj.format(d));
//        插入语句
        return applyDao.insertApplyDate(applyWarehouse);
    }





    public List<ApplyWarehouse> getApplyDate(ApplyWarehouse applyWarehouse){
//        在这里区分仓库类型。
        if (applyWarehouse.getType()==1){
            return applyDao.getSeedApply(applyWarehouse);
        }
        if (applyWarehouse.getType()==2){
            return   applyDao.getToolsApply(applyWarehouse);
        }
        if (applyWarehouse.getType()==3){
            return applyDao.getFertilizerApply(applyWarehouse);
        }
        if (applyWarehouse.getType()==4){
            return   applyDao.getPesticideApply(applyWarehouse);
        }
        if (applyWarehouse.getType()==5){
            return   applyDao.getEquipmentApply(applyWarehouse);
        }
        return  null;
    }
    public int changeApplyState(ApplyWarehouse applyWarehouse){
//        判断账号级别。如果是通过。则还需要改变仓库对应的数值。
        if("root".equals(applyWarehouse.getPageList().get(0).getAccount())&&applyWarehouse.getState()==2){

//            判断仓库类似。因为管理员审批通过。是要更改对应的仓库表的数据的。
         if (applyWarehouse.getType()==1){
             seeds so =new seeds();
//             添加上rootId给so 为了查询总仓库的数据 来判断申请的数量是不是大过了总仓库的量
             so.setId(applyWarehouse.getRootId());
             seeds vo= userDao.selectSeedsWarehouseBySeeds(so).get(0);
             float a=vo.getSeedNum();
             float b=applyWarehouse.getApplyNum();
             if (a>=b){
//                 添加上userId RootId。去查是否已经存在数据了                      这个getSeed判断的时候只有userId和rootId 所以不用管id是不是一致的问题
                 so.setUserId(applyWarehouse.getUserId());
                so.setRootId(applyWarehouse.getRootId());
             // 这里需要做判断。1.先查看申请的仓库是不是已经有这个配件了。如果有，则调用修改方法，如果没有，则调用新增方法。这里不能从审批订单去判断。需要通过查询rootId是否以及存在与对应的数据库
             if(applyDao.getSeed(so)==0){
//一切都准备就绪。到这意味着。总仓库的量大于申请量。并且用户仓库没有对应的种子。
//                        先更改总仓库数量
                    vo.setSeedNum(vo.getSeedNum()-applyWarehouse.getApplyNum());
                    userDao.updateSeedsWarehouse(vo);
//                      下面是对子仓库插入数据   首先。子仓库的rootId =总仓库的主键
                    vo.setRootId(vo.getId());
//                    插入识别的仓库拥有者的id
                    vo.setUserId(applyWarehouse.getUserId());
//                    生成对应的主键
                    vo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//                    插入申请数量
                    vo.setSeedNum(applyWarehouse.getApplyNum());
//                    执行插入语句
                    userDao.insertUserSeedsWarehouse(vo);

                }else{
//                 到这。说明是用户仓库有对应的数据。执行的是更新语句。
//                 依旧需要先减去总仓库的数量
                 vo.setSeedNum(vo.getSeedNum()-applyWarehouse.getApplyNum());
                 userDao.updateSeedsWarehouse(vo);
//                    这里是要获取仓库旧数据 不在SQL语句添加。sql报错不好捕捉。
                    seeds vos =userDao.selectUserSeedsWarehouseByUserId(so).get(0);
//                      更改数据在插入
                    so.setSeedNum(applyWarehouse.getApplyNum()+vos.getSeedNum());
                    userDao.updateUserSeedsWarehouse(so);
                }
             }else {
                 return -3;
             }
            }
            if (applyWarehouse.getType()==2){
                Tools tools =new Tools();
                tools.setId(applyWarehouse.getRootId());
                Tools ztools =userDao.selectToolsByTools(tools).get(0);
                if (ztools.getToolsNum()>=applyWarehouse.getApplyNum()){
                    tools.setUserId(applyWarehouse.getUserId());
                    tools.setRootId(applyWarehouse.getRootId());
                if(applyDao.getTool(tools)==0){
                    ztools.setToolsNum(ztools.getToolsNum()-applyWarehouse.getApplyNum());
                    userDao.updateToolsWarehouse(ztools);
                    ztools.setRootId(ztools.getId());
                    ztools.setUserId(applyWarehouse.getUserId());
                    ztools.setToolsNum(applyWarehouse.getApplyNum());
                    ztools.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    userDao.insertUserToolsWarehouse(ztools);
                }else{
                    ztools.setToolsNum(ztools.getToolsNum()-applyWarehouse.getApplyNum());
                    userDao.updateToolsWarehouse(ztools);
                    Tools vos =userDao.selectUserToolsWarehouseByUserId(tools).get(0);
                    tools.setToolsNum(applyWarehouse.getApplyNum()+vos.getToolsNum());
                    userDao.updateUserToolsWarehouse(tools);
                }
                }else {
                    return -3;
                }
            }
            if (applyWarehouse.getType()==3){
                Fertilizer f =new Fertilizer();
                f.setId(applyWarehouse.getRootId());
                Fertilizer zf =userDao.selectFertilizerByFertilizer(f).get(0);
                if (zf.getFertilizerNum()>=applyWarehouse.getApplyNum()){
                    f.setUserId(applyWarehouse.getUserId());
                    f.setRootId(applyWarehouse.getRootId());
                    if(applyDao.getFertilizer(f)==0){
                        zf.setFertilizerNum(zf.getFertilizerNum()-applyWarehouse.getApplyNum());
                        userDao.updateFertilizerWarehouse(zf);
                        zf.setRootId(zf.getId());
                        zf.setUserId(applyWarehouse.getUserId());
                        zf.setFertilizerNum(applyWarehouse.getApplyNum());
                        zf.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                        userDao.insertUserFertilizerWarehouse(zf);
                    }else{
                        zf.setFertilizerNum(zf.getFertilizerNum()-applyWarehouse.getApplyNum());
                        userDao.updateFertilizerWarehouse(zf);
                        Fertilizer vos =userDao.selectUserFertilizerWarehouseByUserId(f).get(0);
                        f.setFertilizerNum(applyWarehouse.getApplyNum()+vos.getFertilizerNum());
                        userDao.updateUserFertilizerWarehouse(f);
                    }
                }else {
                    return -3;
                }
            }
            if (applyWarehouse.getType()==4){
                Pesticide p =new Pesticide();
                p.setId(applyWarehouse.getRootId());
                Pesticide zp =userDao.selectPesticideByPesticide(p).get(0);
                if (zp.getPesticideNum()>=applyWarehouse.getApplyNum()){
                    p.setUserId(applyWarehouse.getUserId());
                    p.setRootId(applyWarehouse.getRootId());
                    if(applyDao.getPesticide(p)==0){
                        zp.setPesticideNum(zp.getPesticideNum()-applyWarehouse.getApplyNum());
                        userDao.updatePesticideWarehouse(zp);
                        zp.setRootId(zp.getId());
                        zp.setUserId(applyWarehouse.getUserId());
                        zp.setPesticideNum(applyWarehouse.getApplyNum());
                        zp.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                        userDao.insertUserPesticideWarehouse(zp);
                    }else{
                        zp.setPesticideNum(zp.getPesticideNum()-applyWarehouse.getApplyNum());
                        userDao.updatePesticideWarehouse(zp);
                        Pesticide vos =userDao.selectUserPesticideWarehouseByUserId(p).get(0);
                        p.setPesticideNum(applyWarehouse.getApplyNum()+vos.getPesticideNum());
                        userDao.updateUserPesticideWarehouse(p);
                    }
                }else {
                    return -3;
                }
            }
//            if (applyWarehouse.getType()==5){
//                Equipment e = new Equipment();
//                //为e对象添加rootId,用来查询仓库的数据，以判断申请的量是否会超过主仓库所拥有的量
//                e.setId(applyWarehouse.getRootId());
//                //以rootId作为主仓库主键进行查询
//                Equipment ze = equipmentDao.selectEquipmentWarehouseByEquipment(e).get(0);
//                //通过if来判断申请量是否与总仓库即时存量发生冲突
//                //不冲突则进一步判断子仓库是否拥有申请物，有则执行update操作，没有则执行insert操作
//                if (ze.getEquipmentNum()>=applyWarehouse.getApplyNum()){
//                    e.setUserId(applyWarehouse.getUserId());
//                    e.setRootId(applyWarehouse.getRootId());
//                    //判断子仓库是否拥有申请物，有则执行update操作，没有则执行insert操作
//                    if(applyDao.getEquipment(e)==0){
//                        ze.setEquipmentNum(ze.getEquipmentNum()-applyWarehouse.getApplyNum());
//                        equipmentDao.updateEquipmentWarehouse(ze);
//                        //将root表的主键作为主仓库id赋予子仓库
//                        ze.setRootId(ze.getId());
//                        ze.setUserId(applyWarehouse.getUserId());
//                        //将申请的数量赋予对象中
//                        ze.setEquipmentNum(applyWarehouse.getApplyNum());
//                        //为子仓库新建数据生成主键
//                        ze.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//                        //向equipment_warehouse_user插入数据，将ze插入
//                        equipmentDao.insertUserEquipmentWarehouse(ze);
//                    }else{
//                        //当子仓库原来有所提交申请的物品是，user数据库执行的是update操作
//                        //总仓库现有=总仓库原有-提交数量
//                        ze.setEquipmentNum(ze.getEquipmentNum()-applyWarehouse.getApplyNum());
//                        //更新总仓库现有数量
//                        equipmentDao.updateEquipmentWarehouse(ze);
//                        //创建一个新对象获取原仓库的数量
//                        Equipment vos = equipmentDao.selectUserEquipmentWarehouseByUserId(e).get(0);
//                        //将所申请得到的数量加到vos当中，再将值赋予原来对象
//                        e.setEquipmentNum(applyWarehouse.getApplyNum()+vos.getEquipmentNum());
//                        //将申请后的数量更新到user表中
//                        equipmentDao.updateUserEquipmentWarehouse(e);
//                    }
//                }else {
//                    return -3;
//                }
//            }
         return   applyDao.updateApplyState(applyWarehouse);
        }else if(applyWarehouse.getState()==4){
            return   applyDao.updateApplyState(applyWarehouse);
        }else {
            return 0;
        }
    }

    public int changeEquipmentApplyState(EquipmentApply equipmentApply){
        Equipment equipment = new Equipment();
        equipment.setId(equipmentApply.getEquipmentId());
        equipment.setEquipmentStatus(equipmentApply.getState().toString());
        equipmentDao.updateTrading(equipment);
        int i=equipmentDao.insertEquipmentApply(equipmentApply);
        return  i;
    }



}
